#include "message_queue.h"

using namespace boost::interprocess;

CMessageQueue::CMessageQueue()
{
	m_mq = NULL;
}
CMessageQueue::~CMessageQueue()
{
	delete m_mq;
	m_mq = NULL;
}
bool CMessageQueue::Create(const char* name, unsigned msg_num, unsigned msg_size)
{
	try
	{
		message_queue::remove(name);
		m_mq = new message_queue(create_only, name, msg_num, msg_size);
	}
	catch(interprocess_exception &ex){
		std::cout << ex.what() << std::endl;
		return false;
	}
	return true;
}
bool CMessageQueue::Open(const char* name)
{
	try
	{
		m_mq = new message_queue(open_only, name);
	}
	catch(interprocess_exception &ex){
		std::cout << ex.what() << std::endl;
		return false;
	}
	return true;
}
bool CMessageQueue::Send(const char *buf, unsigned len)
{
	try
	{
		m_mq->send(buf, len, 0);
	}
	catch(interprocess_exception &ex){
		std::cout << ex.what() << std::endl;
		return 1;
	}
	return 0;
}
bool CMessageQueue::TryRecv(char* buf, unsigned buf_len , unsigned &len)
{
	try
	{
		unsigned int prioirty = 0;
		return m_mq->try_receive(buf, buf_len, len, prioirty);
	}
	catch(interprocess_exception &ex){
		std::cout << ex.what() << std::endl;
		return false;
	}
	return true;
}